﻿using FantasyDraft2.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FantasyDraft2.Controllers
{
    public class GameGenerateController : Controller
    {
        FantasyDraftEntities fantasyDB = new FantasyDraftEntities();
        //
        // GET: /GameGenerate/

        public ActionResult Index()
        {
            MatchupViewModel mymodel;
            Object c = TempData["mm"];
            if (c != null)
            {
                mymodel = (MatchupViewModel)c;
                mymodel.TeamIdOne = 0;
                mymodel.TeamIdTwo = 0;
                mymodel.teams = fantasyDB.Users.ToList();
            }
            else
            {
                mymodel = new MatchupViewModel
                {
                    TeamIdOne = 0,
                    TeamIdTwo = 0,
                    teams = fantasyDB.Users.ToList(),
                    message = "",
                    oneresults = new List<MatchupResultModel>(),
                    tworesults = new List<MatchupResultModel>()
                };
            }
            return View(mymodel);
        }

        //
        // HTTPPOST
        [HttpPost]
        public ActionResult Index(MatchupViewModel model)
        {
            if (model.TeamIdOne == model.TeamIdTwo)
            {
                MatchupViewModel newmodel = new MatchupViewModel
                {
                    message = "You must pick two different teams!",
                    oneresults = new List<MatchupResultModel>(),
                    tworesults = new List<MatchupResultModel>()
                };

                TempData["mm"] = newmodel;
                return RedirectToAction("Index");
            }
            IEnumerable<Player> listone = fantasyDB.Players.Where(m => m.TeamId == model.TeamIdOne);
            IEnumerable<Player> listtwo = fantasyDB.Players.Where(m => m.TeamId == model.TeamIdTwo);
            User teamone = fantasyDB.Users.SingleOrDefault(m => m.TeamId == model.TeamIdOne);
            User teamtwo = fantasyDB.Users.SingleOrDefault(m => m.TeamId == model.TeamIdTwo);

            List<MatchupResultModel> t1results = new List<MatchupResultModel>();
            List<MatchupResultModel> t2results = new List<MatchupResultModel>();

            Game game = new Game
            {
                TeamId1 = model.TeamIdOne,
                TeamId2 = model.TeamIdTwo,
                Team1 = teamone,
                Team2 = teamtwo
            };

            game = fantasyDB.Games.Add(game);
            fantasyDB.SaveChanges();
            Random r = new Random();
            int goals = 0;
            int assists = 0;
            int activepoints = 0;
            foreach (Player p in listone)
            {
                PlayerGame g = new PlayerGame
                {
                    Assists = r.Next(10),
                    Goals = r.Next(5),
                    GameId = game.GameId,
                    isActive = p.IsActive,
                    Player = p,
                    PlayerId = p.PlayerId,
                    TeamId = p.TeamId,
                    User = p.User
                };
                g = fantasyDB.PlayerGames.Add(g);
                p.Goals += g.Goals;
                p.Assists += g.Assists;
                if (p.IsActive)
                {
                    activepoints += (g.Goals + g.Assists);
                }
                fantasyDB.Entry<Player>(p).State = System.Data.EntityState.Modified;
                goals += g.Goals;
                assists += g.Assists;
                var temp = new MatchupResultModel
                {
                    game = g,
                    player = p,
                    totalscore = g.Assists + g.Goals
                };
                t1results.Add(temp);
            }

            teamone.Score += activepoints;

            fantasyDB.Entry<User>(teamone).State = System.Data.EntityState.Modified;

            game.Score1 = goals + assists;
            goals = 0;
            assists = 0;
            activepoints = 0;
            foreach (Player p in listtwo)
            {
                PlayerGame g = new PlayerGame
                {
                    Assists = r.Next(10),
                    Goals = r.Next(5),
                    GameId = game.GameId,
                    isActive = p.IsActive,
                    Player = p,
                    PlayerId = p.PlayerId,
                    TeamId = p.TeamId,
                    User = p.User
                };
                g = fantasyDB.PlayerGames.Add(g);
                p.Goals += g.Goals;
                p.Assists += g.Assists;
                if (p.IsActive)
                {
                    activepoints += (g.Goals + g.Assists);
                }
                fantasyDB.Entry<Player>(p).State = System.Data.EntityState.Modified;
                goals += g.Goals;
                assists += g.Assists;
                var temp = new MatchupResultModel
                {
                    game = g,
                    player = p,
                    totalscore = g.Assists + g.Goals
                };
                t2results.Add(temp);
            }
            game.Score2 = goals + assists;
            teamtwo.Score += activepoints;
            fantasyDB.Entry<User>(teamtwo).State = System.Data.EntityState.Modified;
            fantasyDB.Entry<Game>(game).State = System.Data.EntityState.Modified;

            fantasyDB.SaveChanges();

            MatchupViewModel nextmodel = new MatchupViewModel
            {
                message = "Matchup done! " + teamone.TeamName + " - " + game.Score1 + "," + teamtwo.TeamName + " - " + game.Score2,
                oneresults = t1results,
                tworesults = t2results
            };

            TempData["mm"] = nextmodel;

            return RedirectToAction("Index");
        }
    }
}
